package baseFx.tests.database.mybatis;

import baseFx.common.Dynamic;
import baseFx.common.Var;
import baseFx.common.concurrent.Task;
import baseFx.common.utils.FxDate;
import baseFx.common.utils.LongId;
import baseFx.database.CursorReader;
import baseFx.database.DbColumn;
import baseFx.database.Transaction;
import baseFx.database.execute.DbExecute;
import baseFx.database.execute.impl.MysqlExecute;
import baseFx.database.mybatis.MybatisContext;
import baseFx.database.mybatis.MybatisDbQuery;
import baseFx.database.mybatis.MybatisSql;
import baseFx.database.mybatis.impl.MysqlQuery;
import baseFx.database.mybatis.impl.MysqlSql;
import baseFx.database.query.PageData;
import baseFx.database.query.PageSet;
import baseFx.serialization.json.JsonUtils;
import baseFx.tests.database.models.BoxMchtInfo;
import com.zaxxer.hikari.HikariDataSource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

public class RunMybatisSql {
    public static void main(String[] args) throws SQLException {
        LongId.set(1, 1);
        MybatisContext ctx = new MybatisContext("baseFx/tests", getDs()) {
            @Override
            protected boolean isShowSql() {
                return true;
            }
        };
        ctx.setFixMysqlCursorProp(true);
        ctx.loadXmlMapper(Arrays.asList("sqlMapper/TestSql.xml"));
        MybatisSql sql = new MysqlSql(ctx);

        DbExecute execute = new MysqlExecute(sql);
        execute.updateBy(Depot.class, s -> s.name = "ddd", w -> {
            w.in(Arrays.asList(1L, 2L, 3L), (i, p) -> i.id = p);
        });

        sql.stmtQuery("test.PageInterceptorTests_1", Dynamic.setNew(s -> {
            s.put("list", Arrays.asList(1, 2, 3, 4, 5));
        }));

//        FxDate date = new FxDate();
//        long t = date.getTime() / 1000 * 1000;
//
//        sql.execute("insert into dict(id,create_time,remark) values(#{id},#{time},'you')", Dynamic.setNew(s -> {
//            s.put("id", LongId.newid());
//            s.put("time", date);
//        }));
//
//        sql.execute("insert into dict(id,create_time,remark) values(#{id},#{time},'mei')", Dynamic.setNew(s -> {
//            s.put("id", LongId.newid());
//            s.put("time", new Date(t));
//        }));
//        MybatisDbQuery query = new MysqlQuery(sql);
//        DbExecute execute = new MysqlExecute(sql);
//        try(Transaction tx = sql.transaction()){
//            BoxMchtInfo mcht = query.getby(BoxMchtInfo.class, i -> {
//                i.code = "1111";
//            }, true);
//            System.out.println(JsonUtils.toJson(mcht));
//        }

//        Pm pm = new Pm();
//        pm.st = FxDate.tryParse("2020-11-5");
//        pm.et = new FxDate(pm.st).addDay(1);
////        PageData<Pos> data = query.stmtPager(Pos.class, new PageSet(), "pmObj", Dynamic.setNew(s -> {
////            s.put("pm", pm);
////        }));
//        PageData<Pos> data = query.stmtPager(Pos.class, new PageSet(), "pmList", Dynamic.setNew(s -> {
//            s.put("snlist", new ArrayList<>(Arrays.asList("1013960353754", "1013960353501", "1013960353502")));
//        }));
//        for (Pos item : data.getData()) {
//            System.out.println(item.sn + "-" + new FxDate(item.examine_time).yyyyMMdd());
//        }
//        System.out.println(data.getDataTotal());

//        query.countBy(Table.class, s -> {
//            s.eq(i -> i.id = 1L);
//        });

//        List<Dynamic> data = sql.stmtQuery(Dynamic.class, "test.depots");
//        for (Dynamic item : data) {
//            System.out.println(item.getString("id"));
//        }
//        Integer c = sql.stmtQueryOne(Integer.class, "test.getcount");
//        System.out.println(c);

//        Var<Integer> index = new Var<>(0);
//        try (CursorReader<Table> reader = sql.stmtCursor(Table.class, "test.allPos")) {
//            for (int t = 0; t < 10; t++) {
//                Table row = reader.next();
//                if (row == null) {
//                    break;
//                }
//                try (MybatisSql mybatisSql = new MysqlSql(ctx)) {
//                    MybatisDbQuery mybatisDbQuery = new MysqlQuery(mybatisSql);
//                    Table rs = mybatisDbQuery.getby(Table.class, i -> i.id = row.id);
//                    System.out.println(index.get() + " = " + rs.id);
//                    index.set(index.get() + 1);
//                }
//                Task.delay(200);
//            }
//        }

//        try (CursorReader<Table> reader = sql.cursor(Table.class, "select * from pos", true)) {
//            while (true) {
//                Table data = reader.next();
//                if (data == null) {
//                    break;
//                }
//                System.out.println(data.id);
//            }
//        }
//        AutoInc inc = new AutoInc();
//        inc.name = UUID.randomUUID().toString();
//        execute.insert(inc);
//        System.out.println(inc.id);
        System.out.println("finish");
        System.exit(0);
    }

    private static DataSource getDs() {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql://192.168.1.55:3306/yunying2?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&serverTimezone=GMT%2B8");
//        ds.setJdbcUrl("jdbc:mysql://test202010211024.rwlb.rds.aliyuncs.com:3306/data_center?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&serverTimezone=GMT%2B8");
        ds.setUsername("dev");
        ds.setPassword("123456");
        ds.setConnectionTimeout(FxDate.toMillisBySeconds(30));
        ds.setMinimumIdle(10);
        ds.setMaxLifetime(FxDate.toMillisBySeconds(5));
        ds.setIdleTimeout(FxDate.toMillisBySeconds(10));
        ds.setConnectionTestQuery("select 1");
        return ds;
    }

    @Resource(name = "auto_inc")
    public static class AutoInc {
        public Integer id;
        public String name;
    }

    public static class Pm {
        public Date st;
        public Date et;
    }

    public static class Pos {
        public Long id;
        public String sn;
        public Date examine_time;
    }

    @Resource(name = "pos")
    public static class Table {
        public Long id;
        public String name;
        public Type type;
    }

    public enum Type {
        A(1), B(2), C(5);

        public final int id;

        Type(int id) {
            this.id = id;
        }
    }

    @Resource(name = "agent_divide_calc_task")
    public static class AgentDivideCalcTask {
        public Long id;
        public Long agent_id;
        public Integer month;
        public Long acct_no;
        public Long deal_time;
        public Long deal_amount;
        public Long group_deal_amount;
        public Long pos_id;
        public Long fr_left;
        public Long fr_right;
        public Long divide_plan_id;
        public Long divide_grade_id;
        public Integer type;
        public Boolean is_executed;
    }
}
